//	Company:	HTHK
//	NAME:	    ProjectGroupExportOptions.java
//	TITLE:	    
//	FUNCTION:	
//	BY:	        Teng Yang
//	DATE:	    09/12/2003
package tss.options;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.ffpc.sql.ConnectionFactory;
import com.util.JdbcUtil;

/**
 * A Class class.
 * <P>
 * 
 * @author HTHK
 */
public class ProjectGroupExportOptions { 
	private Logger logger = Logger.getLogger(ProjectGroupExportOptions.class);
	private List<String> leftList = new ArrayList<String>();
	private List<String> rightList = new ArrayList<String>();

	public ProjectGroupExportOptions() {
		load();
	}

	private void load() {
		ConnectionFactory cf = null;
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		String leftOptionsSql = "select * from tss_c_group";
		String rightOptionsSql = "select * from tss_c_lookup t where t.field_name = 'SELECTED_PROJECTS' order by t.priority";
		try {
			cf = new ConnectionFactory("fasp_tss");
			conn = cf.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(leftOptionsSql);
			while (rs.next()) {
				leftList.add(rs.getString("GROUP_NAME"));
			}

			rs = stmt.executeQuery(rightOptionsSql);
			while (rs.next()) {
				rightList.add(rs.getString("FIELD_VALUE"));
			}

		} catch (Exception e) {
			logger.error(e, e);
		} finally {
			JdbcUtil.release(stmt, rs);
			try {
				if (cf != null)
					cf.finalize();
			} catch (Exception e) {
				logger.error("", e);
			}

		}

	}

	public String getLeftOptions() {
		StringBuffer sb = new StringBuffer();
		List<String> tempList = new ArrayList<String>();
		for(String item : leftList){
			if(!rightList.contains(item)){
				tempList.add(item);
			}
		}
		for(String optionValue : tempList){
			sb.append("<option value='" + optionValue + "'>" + optionValue + "</option>");
		}
		return sb.toString();
	}
	
	public String getRightOptions() {
		StringBuffer sb = new StringBuffer();
		for(String optionValue : rightList){
			sb.append("<option value='" + optionValue + "'>" + optionValue + "</option>");
		}
		return sb.toString();
	}
}
